package com.qfdu.repo.controller;

import com.alibaba.fastjson.JSON;
import com.qfdu.repo.bean.Manager;
import com.qfdu.repo.bean.RespResult;
import com.qfdu.repo.common.Const;
import com.qfdu.repo.common.JSONUtils;
import com.qfdu.repo.common.MybatisUtils;
import com.qfdu.repo.dao.ManagerDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;

/**
 * @author 沐辰
 * @version v1.0
 * @description:
 * @project Qfed2401
 * @Date 2024/5/8 15:12
 * @company qf
 */
@WebServlet(name = "ManagerController",urlPatterns = "/managers")
public class ManagerController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
//        req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if (method != null) {
            if (method.equals("login")) {
                login(req,resp);
            }
        }
    }

    /**
     * 登录功能处理
     * @param req
     * @param resp
     */
    private void login(HttpServletRequest req, HttpServletResponse resp) {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        PrintWriter printWriter = null;
        if (username.length()<4 || password.length()<6) {
            try {
                printWriter = resp.getWriter();
                printWriter.write("信息不合法");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
//        ManagerDao managerDao = new ManagerDaoImpl();
        ManagerDao managerDao = MybatisUtils.getMapper(ManagerDao.class);
        Manager login = managerDao.login(username, password);
        RespResult respResult = new RespResult();
        if (login != null) {
//            JSONUtils.toJSONString(Const.LOGIN_SUCCESS,"登录成功",JSON.toJSONString(login));
            respResult.setCode(Const.LOGIN_SUCCESS);
            respResult.setMsg("登录成功");
            respResult.setData(JSON.toJSONString(login));
        } else {
//            JSONUtils.toJSONString(Const.LOGIN_FAIL,"登录失败");
            respResult.setCode(Const.LOGIN_FAIL);
            respResult.setMsg("登录失败");
        }
        try {
            printWriter = resp.getWriter();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        printWriter.write(JSONUtils.toJSONString(respResult));
        printWriter.flush();
        printWriter.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
